package com.jfinal.config;

import com.jfinal.controller.HelloController;
import com.jfinal.controller.HomeController;
import com.jfinal.controller.LoginController;
import com.jfinal.handler.MyHandler;
import com.jfinal.interceptor.AuthInterceptor;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.model.auth.Auth;
import com.jfinal.model.auth.Role;
import com.jfinal.model.auth.User;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;

/**
 * Created by xiaoyuan on 2018/1/9.
 */
public class Config extends JFinalConfig {

    @Override
    public void configConstant(Constants me) {
        PropKit.use("base_config.txt");
        me.setDevMode(PropKit.getBoolean("devMode",false));
        me.setError404View("/404.html");
    }
    @Override
    public void configRoute(Routes me) {
        me.setBaseViewPath("/WEB-INF/templates");
        me.add("/hello", HelloController.class);
        me.add("/login", LoginController.class);
        me.add("/home", HomeController.class);
    }
    @Override
    public void configEngine(Engine me) {
    }
    @Override
    public void configPlugin(Plugins me) {
        //增加Ehcache
        me.add(new EhCachePlugin());
        //配置Druid连接池
        DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"),
                PropKit.get("user"), PropKit.get("password"));
        me.add(dp);
        //配置ActiveRecord
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        //是否打印sql
        arp.setShowSql(PropKit.getBoolean("devMode",false));
        //添加sql模板
        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath()+"/sql");
        arp.addSqlTemplate("all.sql");
        me.add(arp);
        //添加表-model的映射
        arp.addMapping("user", User.class);
        arp.addMapping("role", Role.class);
        arp.addMapping("authorization",Auth.class);
    }
    @Override
    public void configInterceptor(Interceptors me) {
        me.addGlobalActionInterceptor(new AuthInterceptor());
    }
    @Override
    public void configHandler(Handlers me) {
        me.add(new MyHandler());
    }

}
